{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dbec54e6",
   "metadata": {},
   "source": [
    "# week 05 有序的数据结构之 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f2c06cce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "南院坐落于从化区\n"
     ]
    }
   ],
   "source": [
    "#关于有序性\n",
    "title = \"南院坐落于从化区\"\n",
    "print(title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e9c54955",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_01_name = \"jerny\"\n",
    "#思考：如果想一个变量存放一个班同学的名字，该怎么存？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b91868b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jerny,Alex,Jack,Mork\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = \"Jerny,Alex,Jack,Mork\"\n",
    "print(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "48a3a76d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考2：处理数据会不会比较麻烦？\n",
    "网三_name_list[6:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d0f32df7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思考3：我想知道这个名单有多少个人名？\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31923942",
   "metadata": {},
   "source": [
    "# 引入列表的概念\n",
    "· Lists might contain items of different types, but usually the items all have the same type\n",
    "· （列表可能包含不同类型的项，但通常所有项都具有相同的类型）\n",
    "> 1. 注释：我们已经学习了 数值 和 字符，list可以放数值和字符，但是通常要么一起放数值，要么一起放字符串。\n",
    "> 2. 总结一些通用的有序数据结构的方法：\n",
    ">> 1. 取值：通过索引值取出对应的内容\n",
    ">> 2. 切片：通过索引值区间取出对应的一些内容\n",
    ">> 3. in 可以查看成员对象是否存在\n",
    " > 3. 列表的一些方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b14fdfaa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0d81b8e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Jerny', 'Alex', 'Jack', 'Mork'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = [\"Jerny\",\"Alex\",\"Jack\",\"Mork\"]\n",
    "print(网三_name_list,type(网三_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "802e9b02",
   "metadata": {},
   "source": [
    "## 1.1 列表的运算法则（Operation）\n",
    "· 列表的数据本身不做修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "31dfbb19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jerny'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出 网三_name_list 中第一位同学的名字\n",
    "网三_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6d903eef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看 网三_name_list 有多少位同学\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f1c0d872",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网三： Jerny\n",
      "网三： Alex\n",
      "网三： Jack\n",
      "网三： Mork\n"
     ]
    }
   ],
   "source": [
    "for i in 网三_name_list:\n",
    "    print(\"网三：\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ac7af6f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 可以查询数据是否存在\n",
    "\"Jack\" in 网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "50a04a84",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则（in  +  切片 取值 len(s) s.count）\n",
    "网三_name_list_转专业 = [\"Tucker\",\"Ben\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9990b011",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jerny', 'Alex', 'Jack', 'Mork', 'Tucker', 'Ben']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list + 网三_name_list_转专业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "88d3ae24",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：一个list  可不可以出现 重复项？\n",
    "网一_age_list = [18,19,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "292fdd6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 19, 18, 20, 19]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8c6ec4f",
   "metadata": {},
   "source": [
    "## 列表的方法（Function）--对列表数据进行操作\n",
    "> 1. 增\n",
    "> 2. 删\n",
    "> 3. 改\n",
    "> 4. 查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b6bdd242",
   "metadata": {},
   "outputs": [],
   "source": [
    "# append 增加，增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "db9185ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list = [\"Marry\",\"Alex\",\"Jack\",\"周晓明\",\"Galen\",\"Robin\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "945126d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明', 'Galen', 'Robin']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "13c623e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"张志豪\",)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cc21921b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', 'Jack', '周晓明', 'Galen', 'Robin', '张志豪']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "fe27c930",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insert 增加元素到某个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f830ec70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d384df26",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.insert(2,\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0a3d08cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', '李华', 'Jack', '周晓明', 'Galen', 'Robin', '张志豪']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d83a9278",
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove 删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1fcfd91e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a84d3a25",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.remove('Alex')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4ab6352b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Jack', '周晓明', 'Galen', 'Robin', '张志豪']"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "54024d0f",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [34]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43m网三_name_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mAlex\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "网三_name_list.remove('Alex') #已经被删，所以无法识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ae846709",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jack', '周晓明', 'Galen', 'Robin', '张志豪']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  remove 经常和  in  一起使用，先检查成员变量是否存在，然后再remove\n",
    "if \"李华\" in 网三_name_list:\n",
    "    网三_name_list.remove(\"李华\")\n",
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a8f24e0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e8af0e0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "391ecb1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张志豪'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "12dd86a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'周晓明'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4a5f12a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Jack', 'Galen', 'Robin']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "11dc2a72",
   "metadata": {},
   "outputs": [],
   "source": [
    "#实践：请移除19的元素\n",
    "age_list = [18,19,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "78be4456",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1df48516",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1386f6cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#思路1\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "    age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "63bc03e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [19,13,18,19,17,20,20,18,19,16,17,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f924ad8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[19, 13, 18, 19, 17, 20, 20, 18, 19, 16, 17, 20]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d9d6740b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 19\n",
      "2 19\n",
      "6 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[13, 18, 17, 20, 20, 18, 16, 17, 20]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2：pop(index)，先决条件，知道值出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "    if v == 19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8fa0dac",
   "metadata": {},
   "outputs": [],
   "source": [
    "#课后练习：课本P68和P81"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
